package entities.location;

import entities.Trip;
import entities.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "address")
@NamedQueries({
    @NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a"),
    @NamedQuery(name = "Address.findByIdAddress", query = "SELECT a FROM Address a WHERE a.idAddress = :idAddress"),
    @NamedQuery(name = "Address.findByLat", query = "SELECT a FROM Address a WHERE a.lat = :lat"),
    @NamedQuery(name = "Address.findByLog", query = "SELECT a FROM Address a WHERE a.log = :log"),
    @NamedQuery(name = "Address.findByAdress", query = "SELECT a FROM Address a WHERE a.adress = :adress"),
    @NamedQuery(name = "Address.findByCountryId", query = "SELECT a FROM Address a JOIN a.idCountry d WHERE d.idCountry = ?1")})
public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idAddress")
    private Integer idAddress;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 15)
    @Column(name = "lat")
    private String lat;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 15)
    @Column(name = "log")
    private String log;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 500)
    @Column(name = "adress")
    private String adress;
    
    @JoinColumn(name = "idCountry", referencedColumnName = "idCountry")
    @ManyToOne(optional = false)
    private Country idCountry;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idDeparture")
    private Collection<Trip> tripDepartues;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idDestination")
    private Collection<Trip> tripDestinations;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idAddress")
    private Collection<User> addressOfUsers;

    public Address() {
    }

    public Address(Country idCountry, String lat, String log, String address) {
        this.idCountry = idCountry;
        this.lat = lat;
        this.log = log;
        this.adress = address;
        this.tripDepartues = new ArrayList<>();
        this.tripDestinations = new ArrayList<>();
        this.addressOfUsers = new ArrayList<>();
    }

    public Integer getId() {
        return idAddress;
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLog() {
        return log;
    }

    public void setLog(String log) {
        this.log = log;
    }

    public String getAddress() {
        return adress;
    }

    public void setAddress(String adress) {
        this.adress = adress;
    }

    public Country getCountry() {
        return idCountry;
    }

    public void setCountry(Country idCountry) {
        this.idCountry = idCountry;
    }

    public Collection<Trip> getTripDepartues() {
        return tripDepartues;
    }

    public Collection<Trip> getTripCDestinations() {
        return tripDestinations;
    }

    public Collection<User> getUserCollection() {
        return addressOfUsers;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idAddress != null ? idAddress.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Address)) {
            return false;
        }
        Address other = (Address) object;
        return !((this.idAddress == null && other.idAddress != null) || (this.idAddress != null && !this.idAddress.equals(other.idAddress)));
    }

    @Override
    public String toString() {
        return "entities.extra.Address[ idAddress=" + idAddress + " ]";
    }

}
